<template>
  <div>
    <el-row>
      <el-col :span="12" style="text-align:left">
        <span style="font-family:Microsoft;font-size: 15px;font-weight: bold;">公司基本信息</span>
      </el-col>
      <el-col :span="12" style="text-align: right">
        <el-dropdown @command="handleCommand">
          <span><i class="joys-icon-fusionmenu"></i></span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="edit">编辑</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>
    <el-form :model="form" ref="formName" inline style="background-color: #F4F8F9">
      <el-row>
        <el-col :md="8" :lg="6">
          <el-form-item label="编码" style="text-align: left" prop="orgCode"
                        :rules="[{ required: true, message: '请输入编码', trigger: 'blur' }]">
            <ft-input :editable="editable" v-model="form.orgCode"></ft-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="公司名称" style="text-align: left" prop="orgName"
                        :rules="[{ required: true, message: '请输入公司名称', trigger: 'blur' }]">
            <ft-input :editable="editable" v-model="form.orgName"></ft-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="实际办公地址" style="text-align: left" prop="address">
            <ft-input :editable="editable" v-model="form.address"></ft-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="负责人" style="text-align: left" prop="managerId">
            <ft-user-select v-model="form.managerId" :editable="editable"></ft-user-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :md="8" :lg="6">
          <el-form-item label="隶属公司" style="text-align: left" prop="parentId">
            <!--<ft-org :editable="editable" v-model="form.parentId"></ft-org>-->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: right">
      <el-button @click="cancel" v-if="editable" size="small">取消</el-button>
      <el-button @click="save" v-if="editable" type="primary" size="small">保存</el-button>
    </div>
  </div>
</template>

<script>

  import FtOrg from "./FtOrg";

  export default {
    name: "OrgCompany",
    props: ['orgId', 'parentId'],
    components: {FtOrg},
    data() {
      return {
        editable: true,
        form: {status: 'INIT'}
      }
    },
    mounted() {
      this.load()
    },
    methods: {
      handleCommand(value) {
        this.editable = true;
      },
      cancel() {
        if (this.form.orgId) {
          this.$http.get('/joys-rest/joys-master-data/orgs/queryOrgInfo/' + this.form.orgId).then(resp => {
            this.editable = false;
            this.form = resp.body
          }, resp => {
            this.$message.error(resp.body.message)
          })
        } else {
          this.editable = false;
          this.form = {}
        }
      },
      save() {
        if (this.parentId) {
          this.form.parentId = this.parentId
        }
        this.$refs.formName.validate((valid) => {
          if (valid) {
            this.$http.post('/joys-rest/joys-master-data/orgs/save', this.form).then(resp => {
              this.editable = false
              this.form = resp.body
              this.$emit("refresh", {orgId: this.form.orgId, type: 'OrgCompany'})
              this.$message.success('操作成功！')
            }, resp => {
              this.$message.error(resp.body.message)
            })
          } else {
            console.log('error submit!!')
            return false;
          }
        })

      },
      load() {
        if (this.orgId) {
          this.$http.get('/joys-rest/joys-master-data/orgs/queryOrgInfo/' + this.orgId).then(resp => {
            this.form = resp.body
          }, resp => {
            this.$message.error(resp.body.message)
          })
        }
      }
    },
    watch: {
      orgId() {
        this.load()
      }
    }
  }
</script>

<style scoped>

</style>
